from PIL import Image
import os

dir_path = 'C:\\code'
images = [img for img in os.listdir(dir_path) if img.endswith('.jpg') or img.endswith('.png')]


def compare_images(img1, img2):
    diff = Image.open(img1).histogram() - Image.open(img2).histogram()
    threshold = 100
    return sum(diff) < threshold


unique_images = []
for img in images:
    if not any(compare_images(img, img_unique) for img_unique in unique_images):
        unique_images.append(img)
        os.rename(os.path.join(dir_path, img), os.path.join(dir_path, os.path.basename(img)))
